package ch.janotta.slawo.da.algirithm.lifecycle;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.janotta.slawo.da.Background;
import ch.janotta.slawo.da.CharacterClass;
import ch.janotta.slawo.da.Entity;
import ch.janotta.slawo.da.tables.AttributeValuesTable;
import ch.janotta.slawo.utilities.ArrayTools;
import ch.janotta.slawo.utilities.Dice;

public class MagelikeEntityLifecycleHandlingAlgorithm extends AbstractEntityLifecycleHandlingAlgorithm {
  private static final Logger LOGGER = LoggerFactory.getLogger(MagelikeEntityLifecycleHandlingAlgorithm.class);

  @Override
  protected void generateInitialAbilities(Entity entity) {
    int[] initialValues = new int[NUMBER_OF_ABILITIES];
    for (int i = 0; i < NUMBER_OF_ABILITIES; i++) {
      initialValues[i] = AttributeValuesTable.getRandomAttributeValue();
    }
    Arrays.sort(initialValues);

    // Fill the primary abilities with the top three values
    int[] primaryAbilities = new int[NUMBER_OF_PRIMARY_ABILITIES];
    for (int i = 0; i < NUMBER_OF_PRIMARY_ABILITIES; i++) {
      primaryAbilities[i] = initialValues[NUMBER_OF_ABILITIES - 1 - i];
    }
    int[] randomizedPrimaryAbilities = ArrayTools.randomizeArray(primaryAbilities);
    entity.setCunning(randomizedPrimaryAbilities[0]);
    entity.setMagic(randomizedPrimaryAbilities[1]);
    entity.setWillpower(randomizedPrimaryAbilities[2]);

    // Fill the secondary abilities with the remaining values
    int[] secondaryAbilities = new int[NUMBER_OF_SECONDARY_ABILITIES];
    for (int i = 0; i < NUMBER_OF_SECONDARY_ABILITIES; i++) {
      secondaryAbilities[i] = initialValues[i];
    }
    int[] randomizedSecondaryAbilities = ArrayTools.randomizeArray(secondaryAbilities);
    entity.setCommunication(randomizedSecondaryAbilities[0]);
    entity.setConstitution(randomizedSecondaryAbilities[1]);
    entity.setDexterity(randomizedSecondaryAbilities[2]);
    entity.setPerception(randomizedSecondaryAbilities[3]);
    entity.setStrength(randomizedSecondaryAbilities[4]);
  }

  @Override
  protected void generateBackground(Entity entity) {
    Background[] possibleBackgrounds = Background.getPossibleBackgroundsForCharacterClass(CharacterClass.MAGE);
    int lastPossibleBackground = possibleBackgrounds.length;
    int randomBackgroundIndex = Dice.getRandomNumber(lastPossibleBackground) - 1;
    entity.setBackground(possibleBackgrounds[randomBackgroundIndex]);
  }

  @Override
  protected void generateCharacterClass(Entity entity) {
    CharacterClass[] possibleClasses = entity.getBackground().getPossibleClasses();
    if (Arrays.asList(possibleClasses).contains(CharacterClass.MAGE)) {
      entity.setCharacterClass(CharacterClass.MAGE);
    } else {
      int lastPossibleClass = possibleClasses.length;
      int randomClassIndex = Dice.getRandomNumber(lastPossibleClass) - 1;
      entity.setCharacterClass(possibleClasses[randomClassIndex]);
      LOGGER.warn("Best possible classes didn't contain MAGE, used {} instead", possibleClasses[randomClassIndex].name());
    }
  }

  @Override
  protected void performAdditionalLevelUpOperations(Entity entity) {
    // TODO Auto-generated method stub
    
  }
}
